package registnet.records.source;

import grammar.PrerequisiteCruncher;

import java.util.List;
import java.util.Vector;

import registnet.records.CourseTaken;
import registnet.records.Department;
import registnet.records.IPrerequisite;
import registnet.records.Prerequisite;
import registnet.records.Section;
import registnet.records.Student;

public class CourseTakenSource {

	private Student student;
	private String courseCode;
	private Section section;
	private int semesterTaken;
	private int yearTaken;
	private CourseTaken courseTaken;
	
	public CourseTakenSource(Student student, String courseCode, Section section, int semesterTaken, int yearTaken){
		this.student = student;
		this.courseCode = courseCode;
		this.section = section;
		this.semesterTaken = semesterTaken;
		this.yearTaken = yearTaken;
		this.load();
	}
	
	
	public void load(){
		try{
			registnet.dao.Course DAOCourse = new registnet.dao.CoursePeer().getCourse(this.courseCode);
			registnet.dao.Student DAOStudent = new registnet.dao.StudentPeer().getStudent(this.student.getStudentNumber());
			registnet.dao.Grade DAOGrade = new registnet.dao.GradePeer().getGrade(DAOCourse.getCourseId(), this.section.getSectionName(), DAOStudent.getStudentId(), this.semesterTaken, this.yearTaken);
			Department department = new DepartmentSource(DAOCourse.getUnit().getUnitCode()).getDepartment();
			this.courseTaken = new CourseTaken(this.student, this.courseCode, this.section, this.semesterTaken, this.yearTaken);
			this.courseTaken.setCourseName(DAOCourse.getCourseName());
			this.courseTaken.setUnit(DAOCourse.getUnits());
			this.courseTaken.setDepartment(department);
			this.courseTaken.setPrerequisites(this.getPrerequisites(DAOCourse.getPrerequisite()));
			this.courseTaken.setCredit(DAOGrade.getCredit());
			this.courseTaken.setGrade(DAOGrade.getGrade());
			this.courseTaken.setRemarks(DAOGrade.getRemarks());
			this.courseTaken.setReExam(this.getReExam(DAOGrade));
		}catch(Exception e){
			e.printStackTrace();
		}
	}
	
	/**
	 * Gets the prerequisites of the course
	 * 
	 * @param source - The prerequisites of the course, in text
	 * @return	The prerequisites of the course, in list
	 */
	private List<IPrerequisite> getPrerequisites(String source){
		List<IPrerequisite> prerequisites = new Vector<IPrerequisite>();
		List<String> temp = PrerequisiteCruncher.getPrerequisites(source);
		for(int i=0; i<temp.size(); i++){
			Prerequisite prerequisite = new Prerequisite(temp.get(i));
			prerequisites.add(prerequisite);
		}
		return prerequisites;
	}
	
	
	/**
	 * Gets the removal/completion grade of the course taken
	 * 
	 * @param grade
	 * @return 	The removal/completion grade of the course taken
	 */
	private double getReExam(registnet.dao.Grade grade){
		Double temp;
		if ((temp = new registnet.dao.GradePeer().getRemovalGrade(grade)) != null)
			return temp;
		else
			return 0;
	}
	
	
	/**
	 * Gets the course taken
	 * 
	 * @return	The course taken
	 */
	public CourseTaken getCourseTaken(){
		return this.courseTaken;
	}
	
}
